package com.dataporter.gather.web;

import java.util.Date;

import javax.annotation.Resource;

import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.dataporter.gather.server.Temperature;
import com.dataporter.gather.server.TemperatureService;
import com.touchwisdom.modular.util.config.Page;

@Controller
@RequestMapping("/equip")
public class EquipController {
	@Resource
	private TemperatureService temperatureService;

	@RequestMapping("/index.action")
	public String index() {
		return "equip/index";
	}

	@ResponseBody
	@RequestMapping("/list.action")
	public String list(EasyUiPage<Temperature> easyuiPage) {
		Page<Temperature> page = this.temperatureService.findByPage(easyuiPage);
		StringBuilder builder = new StringBuilder("{\"total\":\"");
		builder.append(page.getTotalCount());
		builder.append("\",\"rows\":[");
		Date date = null;
		for (Temperature temperature : page.getResult()) {
			builder.append("{\"id\":\"").append(temperature.getId());
			builder.append("\",\"mid\":\"").append(temperature.getMid());
			builder.append("\",\"msgLen\":\"").append(temperature.getMsgLen());
			date = temperature.getMeasureTime();
			builder.append("\",\"measureTime\":\"");
			if (date != null) {
				builder.append(DateFormatUtils.format(date,
						"yyyy-MM-dd HH:mm:ss"));
			}
			builder.append("\",\"checkSum\":\"").append(
					temperature.getCheckSum());
			builder.append("\",\"measureNumOne\":\"").append(
					temperature.getMeasureNumOne());
			builder.append("\",\"measureNumTwo\":\"").append(
					temperature.getMeasureNumTwo());
			builder.append("\",\"powerNum\":\"").append(
					temperature.getPowerNum());
			builder.append("\",\"serialNum\":\"").append(
					temperature.getSerialNum());
			builder.append("\",\"equipNo\":\"")
					.append(temperature.getEquipNo()).append("\"},");
		}
		if (page.getTotalCount() > 0) {
			builder.deleteCharAt(builder.length() - 1);
		}
		builder.append("]}");

		return builder.toString();
	}
}